package top.zhangjianyong.utils;


import top.zhangjianyong.entity.Photo;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ： zhangjianyong
 * @date ： 2022/6/24 10:40 AM
 */
public class PhotoHolder {

    private List<Photo> comparePhotos = new LimitedQueue<>(1);
    private List<Photo> photos = new ArrayList<>();
    private float percent = 80;

    private float minPercent = 40;


    public List<Photo> getPhotos() {
        return photos;
    }

    public void init(float percent) {
        this.percent = percent;
    }

    public float getPercent() {
        return this.percent;
    }

    public void addPhoto(Photo photo) {
        if (!compare(photo, percent)) {
            photos.add(photo);
            comparePhotos.add(photo);
        }
    }


    /**
     * 匹配到一张相似图片返回true
     *
     * @param photo   图片对象
     * @param percent 比较阈值
     */
    public boolean compare(Photo photo, float percent) {
        List<int[]> datas = photo.getDatas();

        return comparePhotos.parallelStream().anyMatch(p -> {
            List<int[]> pDatas = p.getDatas();
            if (datas.size() != pDatas.size()) throw new RuntimeException("切分图片不一致");
            int size = pDatas.size();


            for (int i = 0; i < size; i++) {
                float compare = PhotoUtil.compare(pDatas.get(i), datas.get(i));
                if (compare < minPercent)
                    return false;
            }
            return true;
        });
    }

    public boolean compareC(Photo photo, float percent) {
        int[] data = photo.getData();
        return comparePhotos.parallelStream().anyMatch(p -> {
            int[] dd = p.getData();
            float compare = PhotoUtil.compare(dd, data);
            System.out.println(compare);
            return compare > percent;
        });
    }
}
